/**
 * 进货单据查询管理初始化
 */
var PurchaseGoods = {
    purchaseGoodsTableId: "PurchaseGoodsTable",	//商品表格id
    purchaseListTableId: "PurchaseListTableId",  //订单列表表格id
    seItem: null,		//选中的条目
    purchaseGoodsTable: null,  //商品表格
    purchaseListTable: null, //订单表格
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
PurchaseGoods.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        {title: '主键', field: 'id', visible: false, align: 'center', valign: 'middle'},
        {title: '商品编码', field: 'goodsCode', visible: true, align: 'center', valign: 'middle'},
        {title: '商品型号', field: 'goodsModel', visible: true, align: 'center', valign: 'middle'},
        {title: '商品名称', field: 'goodsName', visible: true, align: 'center', valign: 'middle'},
        {title: '购进商品数量', field: 'goodsNum', visible: true, align: 'center', valign: 'middle'},
        {title: '采购价', field: 'unitPrice', visible: true, align: 'center', valign: 'middle'},
        {title: '进货总价', field: 'totalPrice', visible: true, align: 'center', valign: 'middle'},
        {title: '商品单位', field: 'goodsUnit', visible: true, align: 'center', valign: 'middle'},
        {title: '清单ID', field: 'purchaseId', visible: true, align: 'center', valign: 'middle'},
        {title: '商品分类ID', field: 'goodsCategoryId', visible: false, align: 'center', valign: 'middle'},
        {title: '创建者', field: 'createBy', visible: false, align: 'center', valign: 'middle'},
        {title: '创建时间', field: 'createDate', visible: true, align: 'center', valign: 'middle'},
        {title: '更新者', field: 'updateBy', visible: false, align: 'center', valign: 'middle'},
        {title: '更新时间', field: 'updateDate', visible: false, align: 'center', valign: 'middle'},
        {title: '备注信息', field: 'remarks', visible: true, align: 'center', valign: 'middle'},
        {title: '删除标记（0：正常；1：删除）', field: 'delFlag', visible: false, align: 'center', valign: 'middle'}
    ];
};

/**
 * 清单表格
 */
PurchaseGoods.initPurchaseListTableColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        {title: '主键', field: 'id', visible: false, align: 'center', valign: 'middle'},
        {title: '订单号', field: 'orderNumber', visible: true, align: 'center', valign: 'middle'},
        {title: '实付金额', field: 'amountPaid', visible: true, align: 'center', valign: 'middle'},
        {title: '应付金额', field: 'amountPayable', visible: true, align: 'center', valign: 'middle'},
        {
            title: '是否支付',
            field: 'state',
            visible: true,
            align: 'center',
            valign: 'middle',
            formatter: function (value, row, index, field) {
                if (value === 1) {
                    return "已支付";
                } else {
                    return "未支付";
                }
            }
        },
        {title: '创建者', field: 'createBy', visible: false, align: 'center', valign: 'middle'},
        {title: '创建时间', field: 'createDate', visible: true, align: 'center', valign: 'middle'},
        {title: '更新者', field: 'updateBy', visible: false, align: 'center', valign: 'middle'},
        {title: '更新时间', field: 'updateDate', visible: false, align: 'center', valign: 'middle'},
        {title: '备注信息', field: 'remarks', visible: true, align: 'center', valign: 'middle'},
        {title: '删除标记（0：正常；1：删除）', field: 'delFlag', visible: false, align: 'center', valign: 'middle'}
    ];
}


/**
 * 设置dom中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
PurchaseGoods.setDomVal = function (key, val) {
    (typeof val === "undefined") ? $("#" + key).val('') : $("#" + key).val(val);
    return this;
}

/**
 * 清空查询条件
 */
PurchaseGoods.clear = function () {
    this
        .setDomVal('orderNumber')
        .setDomVal('state')
        .setDomVal('remarks')
        .setDomVal('bDate')
        .setDomVal('eDate');
}


/**
 * 打开查看进货入库详情
 */
PurchaseGoods.openPurchaseDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '进货入库详情',
            area: ['1000px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/purchaseGoods/purchaseGoods_detail/' + PurchaseGoods.seItem.id+'/'+PurchaseGoods.seItem.orderNumber
        });
        this.layerIndex = index;
    }
};

/**
 * 检查是否选中
 */
PurchaseGoods.check = function () {
    var selected = $('#' + this.purchaseListTableId).bootstrapTable('getSelections');
    if (selected.length === 0) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    } else {
        PurchaseGoods.seItem = selected[0];
        return true;
    }
};


/**
 * 删除进货单据查询
 */
PurchaseGoods.delete = function () {
    if (this.check()) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/purchase/delete", function (data) {
                Feng.success("删除成功!");
                PurchaseGoods.purchaseListTable.refresh();
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("purchaseId", PurchaseGoods.seItem.id);
            ajax.start();
        }
        Feng.confirm("是否刪除该订单? 该订单下的进货商品也会删除.", operation);
    }
};

/**
 * 查询进货单据查询列表
 */
PurchaseGoods.search = function () {
    var queryData = {};
    queryData['orderNumber'] = $("#orderNumber").val();
    queryData['state'] = $("#state").val();
    queryData['remarks'] = $("#remarks").val();
    queryData['bDate'] = $("#bDate").val();
    queryData['eDate'] = $("#eDate").val();
    console.log('查找条件：', JSON.stringify(queryData));

    //刷新表格并带上分页
    // PurchaseGoods.purchaseListTable.refresh(
    //     {
    //         pageNumber: PurchaseGoods.purchaseListTable.getPageNumber(),
    //         pageSize: PurchaseGoods.purchaseListTable.getPageSize(),
    //         url: "/purchase/list",
    //         query: queryData
    //     });
    PurchaseGoods.purchaseListTable.refresh({query: queryData});
};

/**
 * 是否支付下拉框
 */
PurchaseGoods.isPaySelect = function () {
    $("#state").html("<option value=''>全部</option><option value='1'>已支付</option>" +
        "<option value='0'>未支付</option>")
}


$(function () {
    //订单列表表格
    var purchaseListTableColumns = PurchaseGoods.initPurchaseListTableColumn();
    // /purchase/list
    var purchaseListTable = new BSTable(PurchaseGoods.purchaseListTableId, "/purchase/list", purchaseListTableColumns);
    purchaseListTable.setTableHeight(0);
    // purchaseListTable.setContentType("application/json");
    purchaseListTable.setShowColumns(false);
    purchaseListTable.setShowRefresh(false);
    PurchaseGoods.purchaseListTable = purchaseListTable.init();
    //是否支付下拉框
    PurchaseGoods.isPaySelect();

    //商品列表表格
    // var defaultColunms = PurchaseGoods.initColumn();
    // var table = new BSTable(PurchaseGoods.purchaseGoodsTableId, "", defaultColunms);
    // table.setShowRefresh(false);
    // table.setShowColumns(false);
    // table.setPaginationType("client");
    // PurchaseGoods.purchaseGoodsTable = table.init();

});
